package com.zxx.autohomenavi.base;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import com.gyf.immersionbar.ImmersionBar;
import com.zxx.autohomenavi.R;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityOptionsCompat;
import androidx.core.util.Pair;

/**
 * @author zxx on 2021/8/12
 */
public abstract class BaseActivity extends AppCompatActivity {

    protected abstract int getLayoutId();

    protected abstract void initView();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        onCreateStart(savedInstanceState);
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        initView();
        onCreateEnd(savedInstanceState);
    }


    protected void onCreateStart(Bundle savedInstanceState) {
    }

    protected void onCreateEnd(Bundle savedInstanceState) {
    }

    public void startActivity(Class<?> cls, boolean finishThis) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
        if (finishThis) {
            finish();
        }
    }

    public void startActivity(Class<?> target, Pair<View, String>... sharedElements) {
        Intent intent = new Intent(this, target);
        ActivityOptionsCompat transitionActivityOptions = ActivityOptionsCompat
                .makeSceneTransitionAnimation(this, sharedElements);
        startActivity(intent, transitionActivityOptions.toBundle());
    }

    /**
     * ++++++++++++++++++++++++++++++++++++状态栏,导航栏相关++++++++++++++++++++++++++++++++++++++++++
     */
    /*
     *  此方法在setContentView之后调用,如需定制，则设置 enableWhiteStatusBar 为false,然后覆盖customInitStatusBar方法
     */
    @Override
    public void onContentChanged() {
        super.onContentChanged();
        try {
            if (enableStatusBar()) {
                ImmersionBar.with(this)
                        .fitsSystemWindows(fitsSystemWindows())
                        .statusBarColor(statusBarColor())
                        .statusBarDarkFont(statusBarDarkFont())
                        .navigationBarColor(navigationBarColor(), navigationBarAlpha())
                        .navigationBarDarkIcon(navigationBarDarkIcon())
                        .keyboardEnable(keyboardEnable())
                        .init();
            } else {
                customInitStatusBar();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 针对一些页面，如聊天界面，防止沉浸式状态栏全屏和键盘有冲突，键盘弹出时布局无法上移，可以采用此方法
     */
    protected void setStatusBarColorSpecial(@ColorInt int color, Boolean isDarkFont) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().setStatusBarColor(color);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            View decorView = getWindow().getDecorView();
            if (isDarkFont) {
                decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            } else {
                decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            }
        }
    }

    protected void setStatusBarColorInt(@ColorInt int color,boolean isDarkFont) {
        ImmersionBar.with(this)
                .statusBarColorInt(color)
                .statusBarDarkFont(isDarkFont)
                .init();
    }

    protected void initStatusBar(@IdRes int titleBarId, @ColorRes int statusBarColor) {
        if (statusBarColor == 0) {
            statusBarColor = android.R.color.transparent;
        }
        ImmersionBar.with(this)
                .titleBarMarginTop(titleBarId)
                .statusBarColor(statusBarColor)
                .navigationBarColor(navigationBarColor(), navigationBarAlpha())
                .navigationBarDarkIcon(navigationBarDarkIcon())
                .keyboardEnable(keyboardEnable())
                .init();
    }

    protected boolean enableStatusBar() {
        return true;
    }

    protected Boolean fitsSystemWindows() {
        return true;
    }

    protected int statusBarColor() {
        return R.color.white;
    }

    protected int navigationBarColor() {
        return R.color.white;
    }

    protected float navigationBarAlpha() {
        return 0.5f;
    }

    protected boolean statusBarDarkFont() {
        return true;
    }

    protected boolean navigationBarDarkIcon() {
        return true;
    }

    protected boolean keyboardEnable() {
        return true;
    }

    protected void customInitStatusBar() {
    }
}
